package com.example.pixelyear.utils

import android.app.Activity
import android.app.ActivityOptions
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.View
import com.example.pixelyear.ui.act.*

object YxsDisplay {
    const val beanName = "bean_name"


    fun toMain(activity: Activity, isNeedFinish: Boolean) {
        startActivity(activity, clazz = MainActivity::class.java, isNeedFinish = isNeedFinish)
    }

    fun toNote(activity: Activity, bundle: Bundle? = null, requestCode: Int = -1) {
        startActivity(activity = activity, clazz = NoteActivity::class.java, bundle = bundle, requestCode = requestCode)
    }

    fun toEditColor(activity: Activity, requestCode: Int = -1) {
        startActivity(activity, clazz = EditColorActivity::class.java, requestCode = requestCode)
    }

    fun toYearView(activity: Activity, requestCode: Int) {
        startActivity(activity, YearViewActivity::class.java, requestCode = requestCode)
    }

    fun toSetting(activity: Activity) {
        startActivity(activity, SettingActivity::class.java)
    }

    fun toAboutMe(activity: Activity, bundle: Bundle? = null) {
        startActivity(activity, AboutMeActivity::class.java, bundle = bundle)
    }

    fun toPower(activity: Activity) {
        startActivity(activity, PowerActivity::class.java)
    }

    fun toTutorial(activity: Activity) {
        startActivity(activity, TutorialActivity::class.java)
    }

    //带转场动画的跳转
    private fun startActivity(
        activity: Activity, clazz: Class<*>, view: View? = null, str: String = "",
        requestCode: Int = -1, isNeedFinish: Boolean = false, bundle: Bundle? = null
    ) {
        val intent = Intent(activity, clazz)

        if (bundle != null)
            intent.putExtra(beanName, bundle)
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP && view != null && str.isNotEmpty()) {
            if (requestCode != -1)
                activity.startActivityForResult(
                    intent, requestCode, ActivityOptions.makeSceneTransitionAnimation(
                        activity, view,
                        str
                    ).toBundle()
                )
            else
                activity.startActivity(
                    intent, ActivityOptions.makeSceneTransitionAnimation(
                        activity, view,
                        str
                    ).toBundle()
                )
        } else {
            if (requestCode != -1)
                activity.startActivityForResult(intent, requestCode)
            else
                activity.startActivity(intent)
        }
        if (isNeedFinish)
            finish(activity)
    }

    fun finish(activity: Activity) {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP)
            activity.finishAfterTransition()
        else activity.finish()
    }
}
